package com.angel.email.connection;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Store;

import com.angel.email.dtos.EmailMessageDTO;
import com.angel.email.exceptions.EmailAccessException;
import com.angel.email.exceptions.EmailAttachmentException;
import com.angel.email.response.EmailMessagesResponse;
import com.angel.webapp.file.FileInformation;

/**
 *
 * @author William
 *
 */
public class IncomingEmailConnection extends EmailConnection{

	public IncomingEmailConnection(Store store, String name){
		super(store, name);
	}

	protected Message[] getMessagesFor(String folderName, int ...messagesNumber){
		Folder folder = this.getFolder(folderName);
		Message[] messages = null;
		try {
			messages = folder.getMessages(messagesNumber);
		} catch (MessagingException e) {
			throw new EmailAccessException("Error getting messages for folder [" + folderName + "] for messages count [ " + (messagesNumber != null ? messagesNumber.length : 0) + " ]." , e);
		} finally {
			/*try {
				folder.close(false);
			} catch (MessagingException e) {
				throw new EmailAccessException("Error closing folder [" + folderName + "] for messages count [ "+ (messagesNumber != null ? messagesNumber.length : 0) + " ]." , e);
			}*/
		}
		return messages;
	}

	protected Message[] getMessagesFor(String folderName, Integer minimum, Integer maximum){
		Folder folder = this.getFolder(folderName);
		Message[] messages = null;
		try {
			messages = folder.getMessages(minimum, maximum);
		} catch (MessagingException e) {
			throw new EmailAccessException("Error getting messages for folder [" + folderName + "] for messages count [ " + minimum + " TO " + maximum + " ]." , e);
		} finally {
			try {
				folder.close(false);
			} catch (MessagingException e) {
				throw new EmailAccessException("Error closing folder [" + folderName + "] for messages count [ "+ minimum + " TO " + maximum + " ]." , e);
			}
		}
		return messages;
	}

	protected Message getMessageFor(String folderName, Integer messageNumber){
		Message[] messages = this.getMessagesFor(folderName, new int[]{messageNumber});
		return messages != null && messages.length > 0 ? messages[0] : null;
	}

	protected Message[] getMessagesFor(String folderName){
		Folder folder = this.getFolder(folderName);
		Message[] messages = null;
		try {
			messages = folder.getMessages();
		} catch (MessagingException e) {
			throw new EmailAccessException("Error getting all messages for folder [" + folderName + "]." , e);
		} finally {
			try {
				folder.close(false);
			} catch (MessagingException e) {
				throw new EmailAccessException("Error closing folder [" + folderName + "]." , e);
			}
		}
		return messages;
	}

	protected Message[] getMessagesFor(String folderName, Integer maximum){
		return this.getMessagesFor(folderName, 1, maximum);
	}

	public EmailMessagesResponse getMessageForFolder(String folderName, int ...messagesNumber){
		Folder folder = this.getFolder(folderName);
		List<EmailMessageDTO> emailMessagesDTO = new ArrayList<EmailMessageDTO>();
		try {
			Message[] messages = folder.getMessages(messagesNumber);
			for(Message m: messages){
				emailMessagesDTO.add(new EmailMessageDTO(m));
			}
		} catch (MessagingException e) {
			throw new EmailAccessException("Error getting messages for folder [" + folderName + "] for messages count [ " + (messagesNumber != null ? messagesNumber.length : 0) + " ]." , e);
		} finally {
			/*try {
				folder.close(false);
			} catch (MessagingException e) {
				throw new EmailAccessException("Error closing folder [" + folderName + "] for messages count [ "+ (messagesNumber != null ? messagesNumber.length : 0) + " ]." , e);
			}*/
		}
		return new EmailMessagesResponse(emailMessagesDTO);
	}

	public FileInformation getAttachmentForMessage(String folderName, Integer messageNumber) throws EmailAttachmentException {
		Message message = this.getMessageFor(folderName, messageNumber);
		return super.getEmailAttachmentManager().getAttachmentFor(message);
	}

	public EmailMessagesResponse getMessageForFolder(String folderName, Integer messageNumber){
		return this.getMessageForFolder(folderName, new int[]{messageNumber});
	}

	public EmailMessagesResponse getMessagesForFolder(String folderName, Integer maximum){
		return this.getMessagesForFolder(folderName, 1, maximum);
	}

	public EmailMessagesResponse getMessagesForFolder(String folderName, Integer minimum, Integer maximum){
		Folder folder = this.getFolder(folderName);
		List<EmailMessageDTO> emailMessagesDTO = new ArrayList<EmailMessageDTO>();
		try {
			Message[] messages = folder.getMessages(minimum, maximum);
			for(Message m: messages){
				emailMessagesDTO.add(new EmailMessageDTO(m));
			}
		} catch (MessagingException e) {
			throw new EmailAccessException("Error getting messages for folder [" + folderName + "] for messages [ 0 to "+ maximum + " ]." , e);
		} finally {
			try {
				folder.close(false);
			} catch (MessagingException e) {
				throw new EmailAccessException("Error closing folder [" + folderName + "] for messages [ 0 to "+ maximum + " ]." , e);
			}
		}
		return new EmailMessagesResponse(emailMessagesDTO);
	}

	public Object getAttachmentsForMessage(String folderName, Integer messageNumber){
		//Message message = this.getMessageFor(folderName, messageNumber);
		//TODO Do.
		return null;
	}
}
